import { getmenuToModel } from "@/api/model";
const state = {
  approverDrawer: false, //审核人弹窗
  updateNodeId: "", //需要更新的节点id
  nodeConfig: {}, //节点参数
  operations: {},
  formPermission: "",
  roleList: [],
  businessDrawer: false,
  businessConfig: null,
  formType: "",
  taskId: "",
  previewConfig: {},
};
const mutations = {
  SET_TASKID(status, payload) {
    status.taskId = payload;
  },
  SET_PREVIEWCONFIG(status, payload) {
    status.previewConfig = payload;
  },
  SET_BUSINESSCONFIG(status, payload) {
    status.businessConfig = payload;
  },

  SET_BUSINESSDRAWER(status, payload) {
    status.businessDrawer = payload;
    if(!payload){
      status.businessConfig = null
    }
  },
  SET_BUSINESSTYPE(status, payload) {
    status.formType = payload;
  },
  setApproverDrawer(status, payload) {
    status.approverDrawer = payload;
  },
  SETUPDATENODEID(status, { id, operations, formPermission, roleList }) {
    status.updateNodeId = id;
    status.operations = operations;
    status.formPermission = formPermission;
    status.roleList = roleList;
  },
  SET_NODECONFIG(status, payload) {
    status.nodeConfig = payload;
  },
  UPDATENODE(status, payload) {
    status.nodeConfig = payload;
  },
  ADDNODE(status, payload) {
    status.nodeConfig = payload;
  },
  DELNODE(status, payload) {
    status.nodeConfig = payload;
  },
};
const actions = {
  setNodeConfig({ commit, state }, nodeConfig) {
    commit("SET_NODECONFIG", nodeConfig);
  },
  setUpdateNodeId({ commit, state }, id) {
    let updateNodeConfig = JSON.parse(JSON.stringify(state.nodeConfig));
    let operations = {};
    let formPermission = "";
    let roleList = [];
    const checkNode = (nodeConfig) => {
      if (!nodeConfig.child) {
        return;
      }
      if (nodeConfig.child.id === id) {
        operations = nodeConfig.child.operations;
        formPermission = nodeConfig.child.formPermission;
        roleList = nodeConfig.child.roleList;
      } else {
        checkNode(nodeConfig.child);
      }
    };
    checkNode(updateNodeConfig);
    commit("SETUPDATENODEID", { id, operations, formPermission, roleList });
  },
  //更新node
  updateNode({ commit, state }, params) {
    let updateNodeConfig = JSON.parse(JSON.stringify(state.nodeConfig));
    const checkNode = (nodeConfig) => {
      if (!nodeConfig.child) {
        return;
      }
      if (nodeConfig.child.id === state.updateNodeId) {
        nodeConfig.child = {
          ...nodeConfig.child,
          ...params,
        };
        return;
      } else {
        checkNode(nodeConfig.child);
      }
    };
    checkNode(updateNodeConfig);
    commit("UPDATENODE", updateNodeConfig);
  },
  //添加node
  addNode({ commit, state }, { id, child }) {
    let timeId = "test_" + Date.now();
    let updateNodeConfig = JSON.parse(JSON.stringify(state.nodeConfig));
    const checkNode = (nodeConfig) => {
      if (nodeConfig.id === id) {
        if (child) {
          child.pid = timeId;
        }
        let childs = {
          type: "approval",
          id: timeId,
          pid: id,
          name: "审核人",
          assigneeType: "role",
          operations: {
            complete: true,
            refuse: true,
            back: true,
            // delegate: true,
          },
          formPermission: "readonly",
          roleList: [],
          roles: [],
          child,
        };
        nodeConfig.child = childs;
        return;
      } else {
        checkNode(nodeConfig.child);
      }
    };
    checkNode(updateNodeConfig);
    commit("ADDNODE", updateNodeConfig);
  },

  delNode({ commit, state }, { child, id }) {
    let updateNodeConfig = JSON.parse(JSON.stringify(state.nodeConfig));
    const checkNode = (nodeConfig) => {
      console.log("delNode", nodeConfig);
      if (!nodeConfig.child) {
        return;
      }
      if (nodeConfig.child.id === id) {
        if (child) {
          child.pid = nodeConfig.child.pid;
        }
        nodeConfig.child = child;
        return;
      } else {
        checkNode(nodeConfig.child);
      }
    };
    checkNode(updateNodeConfig);
    commit("UPDATENODE", updateNodeConfig);
  },

  //保存菜单查询model数据
  getModel({ commit,state }, { type, menuId,formData,businessType}) {
        if(!menuId){
            this.$message.error("没有关联此业务！");
            return;
        }
        getmenuToModel(menuId).then((res) => {
        if (res.code === 200) {
        const config = {
            ...res.data,
            menuId,
            formData,
            businessType,
            modelData: JSON.parse(res.data.modelData),
        };
        commit("SET_BUSINESSCONFIG", config);
        commit("SET_BUSINESSDRAWER", true);
        commit("SET_BUSINESSTYPE", type);
        }
      });
  },

  //set model数据
  setModel({ commit }, { config, type, taskId }) {
    if (taskId) {
      commit("SET_TASKID", taskId);
    }
    commit("SET_BUSINESSCONFIG", config);
    commit("SET_BUSINESSDRAWER", true);
    commit("SET_BUSINESSTYPE", type);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
